import React, { Component } from 'react';
import {
    BackHandler,
    ToastAndroid,
} from 'react-native';

// 设置返回按钮
const lastBackPressed = undefined;
export default (props) => (View) => class Back extends Component {
    componentWillMount() {

        console.log('2222222222222222222222')
        BackHandler.addEventListener('hardwareBackPress', this._onBackAndroid);
    }
    componentWillUnmount() {
        console.log('我卸载了啊啊啊啊')
        BackHandler.removeEventListener('hardwareBackPress', this._onBackAndroid);
    }
    _onBackAndroid = () => {
        console.log(props)
        console.log(this.props)
        console.log('asdasdasdasd')
        
        if (this.props.navigation.state.routeName == "App") {
            if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
                //最近2秒内按过back键，可以退出应用。
                BackHandler.exitApp()
                return false
            }
            this.lastBackPressed = Date.now();
            ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT);
            return true;
        } else {
            return false;//默认行为
        }

    }
    render() {
        return (
            <View {...this.props} />
        )
    }
}

